<!--
 * @Author: ChunLai
 * @Date: 2022-01-17 11:24:24
 * @LastEditTime: 2025-09-02 09:49:27
 * @Description: 订单确认页
 * @FilePath: \02.bldinsure\src\views\sign\bill.vue
-->
<template>
  <div class="bill_sure_page">
    <div class="check_status">
      <TipMsg msg="请认真核对投保信息，以防影响正常投保！" />
      <!-- 核保状态显示 -->
      <InsureStatus ref="insureFlagRef" />
    </div>
    <div class="mt10">
      <RiskPlanModule />
    </div>
    <!-- 投保信息 -->
    <InsureInfos />
    <!-- 业务员信息(双录展示) -->
    <!-- <DoubleRecord
      ref="doubleRecordRef"
      v-if="agentInfo.certificate && ['1'].includes(isNetInsure)"
    /> -->
    <ProtocolsReading
      id="protocols_module"
      v-if="['1', '3'].includes(isNetInsure)"
    />
    <template v-if="isNetInsure == '2'">
      <!-- 签名客户列表 -->
      <div class="notice-mode-head mt10">
        <span class="head-box">请完成以下签字</span>
      </div>
      <QianZiSingleListModule
        ref="qianziSingle"
        v-model="signStatus"
        v-if="singleFileSign"
      />
      <QianZiListModule ref="qianziList" v-model="signStatus" v-else />
    </template>
    <!-- 核保成功，可转人核(不可修改;转人核/支付页) -->
    <FooterBtns
      btnNum="2"
      leftText="转人核"
      @saveField="renWriteApi"
      rightText="下一步"
      @saveForm="toPayPage()"
      v-if="underwriteFlag == '6'"
    ></FooterBtns>
    <FooterBtns
      btnNum="2"
      leftText="返回修改"
      @saveField="$router.push('/form')"
      rightText="去核保"
      @saveForm="goUnderWrite"
      v-else-if="underwriteFlag == '0'"
    ></FooterBtns>
    <FooterBtns
      btnNum="1"
      :rightText="btnText"
      @saveForm="goUnderWrite"
      v-else
    ></FooterBtns>
    <SmsCodeInput
      ref="smsCodeRef"
      @handleCheck="payAuthSureApi"
      v-if="smspop"
    />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { ImagePreview } from "vant";
import RiskPlanModule from "@/components/RiskPlanModule";
import InsureInfos from "./components/InsureInfos";
import QianZiListModule from "./components/QianZiListModule";
import QianZiSingleListModule from "./components/QianZiSingleListModule";
import DoubleRecord from "./components/DoubleRecord";
import InsureStatus from "./components/InsureStatus";
import ProtocolsReading from "./components/ProtocolsReading";
import SmsCodeInput from "@/views/pay/components/SmsCodeInput";
import { underwrite, toArtificial } from "@/utils/functions.js";
import { billHandleMixin } from "./hebao.js";
export default {
  name: "billSurePage",
  data() {
    return {
      smspop: false,
      signStatus: false,
    };
  },
  components: {
    RiskPlanModule,
    InsureInfos,
    QianZiListModule,
    DoubleRecord,
    InsureStatus,
    ProtocolsReading,
    QianZiSingleListModule,
    SmsCodeInput,
  },
  computed: {
    ...mapGetters({
      agentInfo: "agent/agentInfo",
      payApiReg: "infoReg/payApiReg",
      insu3Status: "json/insu3Status",
      totalPrem: "insert/totalPrem",
      renewalInfo: "insert/renewalInfo",
      isNeedDouble: "insert/isNeedDouble",
      underwriteFlag: "insert/underwriteFlag",
      readFinish: "readings/wholeFinish",
      singleFileSign: "page/singleFileSign",
      dRecordPassFlag: "page/dRecordPassFlag",
      doubRecordAbutment: "page/doubRecordAbutment",
    }),
    btnText() {
      let res = "签名确认";
      if (this.signStatus) {
        if (this.underwriteFlag == "") {
          res = "去核保";
        } else if (this.underwriteFlag === "2") {
          res = "转人核";
        } else {
          res = this.underwriteFlag === "7" ? "申请打款" : "下一步";
        }
      }
      if (["1", "3"].includes(this.isNetInsure)) {
        if (this.underwriteFlag === "1") {
          res = "去支付";
        } else {
          res = "提交订单";
        }
        if (this.compEngName == "ansheng") {
          if (this.insu3Status == "3") res = "去支付";
        }
      }
      return res;
    },
    // 配置双录+需要双录+质检未通过
    dRecordFinish() {
      let res = true;
      if (
        this.doubRecordAbutment &&
        this.isNeedDouble == "1" &&
        this.dRecordPassFlag == "0"
      ) {
        res = false;
      }
      return res;
    },
  },
  watch: {},
  created() {
    this.$store.commit("filesign/SET_STATE_CONFIG", {
      openSign: false,
    });
    // 网销
    if (["1", "3"].includes(this.isNetInsure)) {
      this.$store.dispatch("readings/initFilesList");
    }
  },
  mounted() {
    if (this.isNetInsure == "2") {
      if (this.singleFileSign) {
        this.$refs.qianziSingle.getStatus();
      } else {
        this.$refs.qianziList.getStatus();
      }
    }
    this.$refs.insureFlagRef.initModule();
  },
  mixins: [billHandleMixin],
  methods: {
    viewImg(imgurl) {
      if (imgurl) {
        ImagePreview([imgurl]);
      } else {
        this.$toast("未获取到职业资格证图片");
      }
    },
    goUnderWrite() {
      if (this.isNetInsure == "2") {
        if (!this.signStatus) return this.$toast("请完成签名");
        if (!this.dRecordFinish) return this.$toast("双录待质检,请稍后操作");
      } else if (["1", "3"].includes(this.isNetInsure)) {
        if (!this.readFinish) {
          let target = document.querySelector("#protocols_module");
          target.scrollIntoView({
            behavior: "smooth",
            block: "start",
          });
          return this.$toast("请完成以上条款的阅读");
        }
      }
      this.nextStepPage();
    },
    nextStepPage() {
      if (this.compEngName == "ansheng") {
        // 核保成功，签约失败1
        if (this.insu3Status == "1") return this.netBillSuccess();
        if (this.insu3Status == "3") return this.netPayApi();
      }
      if (["1", "4"].includes(this.underwriteFlag)) {
        if (["1", "3"].includes(this.isNetInsure)) {
          this.netBillSuccess();
        } else {
          this.toPayPage();
        }
      } else if (["2"].includes(this.underwriteFlag)) {
        this.renWriteApi();
      } else if (["3", "5", "-1"].includes(this.underwriteFlag)) {
        this.$router.push("/billres");
      } else if (["7"].includes(this.underwriteFlag)) {
        this.toRemitPage();
      } else {
        this.underwriteApi();
      }
    },
    underwriteApi() {
      this.$store.commit("updateLoad", true);
      this.$store.commit("updateLoadTxt", "正在核保中，请稍等");
      underwrite(this.apiComParams).then(
        ({ data }) => {
          this.$store.commit("updateLoad", false);
          this.$store.commit("insert/SET_UNDERWRITE_FLAG", {
            code: data.code,
          });
          this.$refs.insureFlagRef.initModule(); // 初始化保单状态提示
          if (data.contractNum) {
            this.$store.commit("insert/SET_CONTRACT_NUM", data.contractNum);
          }
          // 7表示"待打款"
          if (!["0", "2"].includes(data.code)) {
            this.$store.commit("insert/SET_FORBID_ALTER", true);
          }
          if (["2", "6"].includes(data.code)) {
            this.$baseConfirm(
              data.msg,
              () => {
                this.renWriteApi();
              },
              () => {
                if (data.code == "6") {
                  this.toPayPage();
                }
              },
              "转人核",
              `${data.code == "6" ? "下一步" : "取消"}`
            );
          } else if (["7"].includes(data.code)) {
            this.$baseConfirm(
              data.msg,
              () => {
                this.toRemitPage();
              },
              () => {
                // this.$router.push("/billres");
              },
              "申请打款",
              `再看看`
            );
          } else {
            this.$baseAlert(data.msg, () => {
              this.heBaoToDo(data);
            });
          }
        },
        (err) => {
          this.$store.commit("updateLoad", false);
        }
      );
    },
    heBaoToDo(data) {
      if (["1"].includes(data.code)) {
        // 网销，直接跳转收银台
        if (data.payUrl) {
          window.location.href = data.payUrl;
        } else if (["1", "3"].includes(this.isNetInsure)) {
          this.netBillSuccess();
        } else {
          this.toPayPage();
        }
      }
    },
    // 人核
    renWriteApi() {
      this.$store.commit("updateLoad", true);
      toArtificial(this.apiComParams).then(
        ({ data }) => {
          this.$store.commit("updateLoad", false);
          this.$baseAlert(data.msg, () => {
            if (data.code === "1") {
              this.$router.push("/billres");
            }
          });
        },
        (err) => {
          this.$store.commit("updateLoad", false);
        }
      );
    },
    toPayPage() {
      this.$router.push("/pay");
    },
    toRemitPage() {
      this.$store.dispatch("common/toBldRemitPage");
    },
  },
};
</script>

<style lang="less" scoped>
.bill_sure_page {
  padding-bottom: 100px;
}
</style>
